package com.zl.system.controller;

import com.zl.basic.util.AjaxResult;
import com.zl.basic.util.PageList;
import com.zl.system.domain.DictionaryDetail;
import com.zl.system.query.DictionaryDetailQuery;
import com.zl.system.service.DictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionaryDetailController {
    @Autowired
    private DictionaryDetailService dictionaryDetailService;
    /**
     * 查询一个对象的数据
     * */
    @GetMapping("/{id}")
    public DictionaryDetail findOne(@PathVariable("id") Long id) {
        return dictionaryDetailService.findById(id);
    }
    /**
     * 查询所有
     * */
    @GetMapping
    public List<DictionaryDetail> findAll() {
        return dictionaryDetailService.findAll();
    }
    /**
     * 分页查询
     * */
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery) {

        return dictionaryDetailService.queryPage(dictionaryDetailQuery);
    }

    /**
     * 删除操作
     * */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            dictionaryDetailService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    /**
     * 添加或者修改操作
     * */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody  DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId()==null){
                dictionaryDetailService.save(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！！！");
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }
}
